package com.nari.ncps.screen.service.impl.coal;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nari.ncps.screen.entity.coal.CoalOpermonRtOutput;
import com.nari.ncps.screen.mapper.coal.CoalOpermonRtOutputMapper;
import com.nari.ncps.screen.service.coal.CoalOpermonRtOutputService;
import com.nari.ncps.screen.vo.coal.CoalProductionOutputVO;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 运行检测产出情况表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class CoalOpermonRtOutputServiceImpl extends ServiceImpl<CoalOpermonRtOutputMapper, CoalOpermonRtOutput> implements CoalOpermonRtOutputService {

    @Override
    public List<CoalProductionOutputVO> getProductionOutput(Integer year) {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        
        // 计算7天前的日期
        LocalDate sevenDaysAgo = currentDate.minusDays(6);
        
        // 查询最近7天的数据
        LambdaQueryWrapper<CoalOpermonRtOutput> wrapper = Wrappers.lambdaQuery();
        wrapper.ge(CoalOpermonRtOutput::getYear, sevenDaysAgo.getYear())
               .ge(CoalOpermonRtOutput::getMonth, sevenDaysAgo.getMonthValue());
//               .orderByAsc(CoalOpermonRtOutput::getDate);
        
        List<CoalOpermonRtOutput> outputList = this.list(wrapper);
        
        // 按日期分组取数据
        Map<String, CoalOpermonRtOutput> dailyDataMap = new HashMap<>();
        
        for (CoalOpermonRtOutput output : outputList) {
            String dateKey = output.getYear() + "-" + output.getMonth() + "-" + output.getDay();
            // 只保留每天的第一条记录
            if (!dailyDataMap.containsKey(dateKey)) {
                dailyDataMap.put(dateKey, output);
            }
        }
        
        // 转换为VO列表
        List<CoalProductionOutputVO> resultList = new ArrayList<>();
        
        // 生成最近7天的数据（按照日期从小到大的顺序）
        for (int i = 6; i >= 0; i--) {
            LocalDate targetDate = currentDate.minusDays(i);
            String dateKey = targetDate.getYear() + "-" + targetDate.getMonthValue() + "-" + targetDate.getDayOfMonth();
            
            CoalProductionOutputVO vo = new CoalProductionOutputVO();
            vo.setYear(targetDate.getYear());
            vo.setMonth(targetDate.getMonthValue());
            vo.setDay(targetDate.getDayOfMonth());
            vo.setDate(java.sql.Date.valueOf(targetDate));
            
            CoalOpermonRtOutput output = dailyDataMap.get(dateKey);
            if (output != null) {
                vo.setRealtimeOutput(output.getRealtimeOutput());
                vo.setRealtimeStock(output.getRealtimeStock());
            }
            
            resultList.add(vo);
        }
        
        return resultList;
    }
}
